﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.Utility.Common.DataBase;

namespace Zhuang.WinForm.AuthorityManager
{
    public partial class UserManager : Form
    {
        public UserManager()
        {
            InitializeComponent();
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            doSearch();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            doSearch();
        }

        private void doSearch()
        {
            string condiction = tbCondiction.Text.Trim() ;
            
            string strSql = "select userid,username,userpassword from App_User where username like '%" + condiction + "%'";

            dataGridView1.DataSource = SqlHelper.Query(strSql).Tables[0];
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserAddOrUpdate uaou = new UserAddOrUpdate();
            if (uaou.ShowDialog() == DialogResult.OK)
            {
                doSearch();
            }
            
        }

        private void btnModify_Click(object sender, EventArgs e)
        {

            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选择要修改的用户！");
                return;
            }


            UserAddOrUpdate uaou = new UserAddOrUpdate();
            uaou.userid = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["userid"].Value.ToString().Trim());
            uaou.userName = dataGridView1.SelectedRows[0].Cells["username"].Value.ToString().Trim();
            uaou.userPassowrd = dataGridView1.SelectedRows[0].Cells["userpassword"].Value.ToString().Trim();

            if (uaou.ShowDialog() == DialogResult.OK)
            {
                doSearch();
            }
                
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选择要删除的用户！");
                return;
            }

            if (MessageBox.Show("确定要删除用户“"+dataGridView1.SelectedRows[0].Cells["username"].Value.ToString().Trim()+"”吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    string strUserId=dataGridView1.SelectedRows[0].Cells["userid"].Value.ToString().Trim();

                    string strSql = "delete App_User where userid=" + strUserId;
                    strSql = strSql + ";delete App_UserToMenu where userid=" + strUserId;

                    SqlHelper.ExecuteSql(strSql);

                    doSearch();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败！"+ex.Message);

                }
            }
        }
    }
}
